package cn.yht.wang.controller;

import cn.yht.wang.bean.SysUser;
import cn.yht.wang.common.ApiResult;
import cn.yht.wang.mapper.UserMapper;
import cn.yht.wang.utils.JWTUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "用户接口")
@RestController
@RequestMapping("/api/account")
public class AccountController {
    @Autowired
    UserMapper userMapper;
//  @Autowired
//  JdbcTemplate jdbcTemplate;

    Logger logger = LoggerFactory.getLogger(UserController.class);

    @ApiOperation(value = "用户登录 获取token", notes="登陆获取token")
    @PostMapping(value="/login")
    public ApiResult<Map<String,Object>> login(@RequestBody SysUser user){
        SysUser result = userMapper.queryUserById(user.getUserName(),user.getPassword());
        Map<String,Object> map = new HashMap<>();
        try
        {
            SysUser userDb = result;
            Map<String,String> payload = new HashMap<>();
            payload.put("id",userDb.getId().toString());
            payload.put("name",userDb.getUserName());
            //生成jwt的令牌
            String token = JWTUtils.getToken(payload);
            map.put("state",true);
            map.put("msg","认证成功");
            map.put("token",token);
        }
        catch (Exception e)
        {
            map.put("state",false);
            map.put("msg",e.getMessage());
        }
        //System.out.println("测试新电脑git 功能！");
        return new ApiResult<>("0000", "登陆成功.", map);
    }

}
